package cetcbigdata.da.service.hainan.util;

import com.alibaba.fastjson.JSONObject;

import static cetcbigdata.da.base.Base.*;
import static cetcbigdata.da.service.hainan.util.ServiceHNUtil.*;

public class AllInfo {
    private String id;
    private String allInfoUrl;
    private String sourcekey;
    private String responce;
    private JSONObject allInfoHeader;
    private JSONObject allInfoParmas;

    public String getResponce() {
        return responce;
    }

    public void setResponce(String responce) {
        this.responce = responce;
    }

    public String getAllInfoUrl() {
        return allInfoUrl;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSourcekey() {
        return sourcekey;
    }

    public JSONObject getAllInfoHeader() {
        return allInfoHeader;
    }

    public JSONObject getAllInfoParmas() {
        return allInfoParmas;
    }

    public void setAllInfoUrl(String allInfoUrl) {
        this.allInfoUrl = allInfoUrl;
    }

    public void setSourcekey(String sourcekey) {
        this.sourcekey = sourcekey;
    }

    public void setAllInfoHeader(JSONObject allInfoHeader) {
        this.allInfoHeader = allInfoHeader;
    }

    public void setAllInfoParmas(JSONObject allInfoParmas) {
        this.allInfoParmas = allInfoParmas;
    }

    public void init(){
        setAllInfoUrl("https://wssp.hainan.gov.cn/wssp3/bszn/exempt/allInfo");
        JSONObject header = new JSONObject();
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
        header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?id="+getId()+"&sourcekey="+getSourcekey());
        setAllInfoHeader(header);

        JSONObject parmas = new JSONObject();
        parmas.put("id", getSourcekey());
        parmas.put("type", "1");
        setAllInfoParmas(parmas);
    }

    public JSONObject getAllInfo() throws Exception{
        JSONObject result = new JSONObject();
        String strResponce = postFormReq(getAllInfoUrl(),getAllInfoHeader(),getAllInfoParmas());
        setResponce(strResponce);
        JSONObject responce = JSONObject.parseObject(strResponce);
        if (responce.getInteger("code") == 20000 && responce.getJSONObject("data")!=null) {

            JSONObject sxImplBasic = responce.getJSONObject("data").getJSONObject("sxImplBasic");
            //开头
            result.put("名称", sxImplBasic.getString("taskname"));

            //url
            result.put("地址", "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?id="+id+"&sourcekey="+sourcekey);

            //基本信息
            result.put("基本信息", getBasicInfo(sxImplBasic));

            //设定依据
            result.put("设定依据", isNotBlankOrNull(sxImplBasic.getString("bylaw")) ? sxImplBasic.getString("bylaw") : "无");

            //受理条件
            result.put("受理条件", isNotBlankOrNull(sxImplBasic.getString("acceptcondition")) ? sxImplBasic.getString("acceptcondition") : "无");

            //办理材料
            result.put("办理材料", getMaterialsInfo(responce.getJSONObject("data").getJSONArray("materials")));

            //办理流程
            result.put("办理流程", getflowHJsInfo(responce.getJSONObject("data").getJSONArray("flowHjs"),
                    responce.getJSONObject("data").getJSONObject("flow").getString("flowid")));

            //收费内容
            result.put("收费内容", getFeeInfo(responce.getJSONObject("data").getJSONArray("feeProjects")));

            //常见问题
            result.put("常见问题", getQuestionInfo(responce.getJSONObject("data").getJSONArray("questions")));

            return result;
        }else {
            result.put("error","连接失败");
            return result;
        }
    }

    public static void main(String[] args) {
        AllInfo allInfo = new AllInfo();
        try {
            allInfo.getAllInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
